<?php
namespace models\service;
//use PEAR;

class InboxService {

	protected $ci;
	public $lastBerkas;
	
	public function __construct() {
		$this -> ci = &get_instance();
	}

	public function startNewFlow($berkas, $profile, $jenisProcedure=0){
		$inbox = new \models\entities\Inbox;
		$inbox->setId($berkas);
		
		// HARDCODE, use to be table jenis_prosedur 
		if ($jenisProcedure==0){
			$criteria = array('profile' => $profile);
			$procedureStep = $this->ci->em->getRepository('\models\entities\ProcedureStep')->findBy($criteria);
			$sps=0;
			
			foreach ($procedureStep as $ps){
				if ($ps->getId() == 1 || $ps->getId() == 5){
					$sps = $ps;
				}
			}
			
			$inbox->setProcedureStep($sps);
			$inbox->setCondition("START");
			$berkas->setStartDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
			
			$berkasActivity = new \models\entities\BerkasActivity;
			$berkasActivity->setBerkas($berkas);
			$berkasActivity->setProcedureStep($sps);			
			$berkasActivity->setAction("START");			
			$berkasActivity->setProfile($profile);
			$user = CurrentUser::user();
			$berkasActivity->setUser($user->getUserId());
		}
		try {
			$this->ci->em->persist($berkas);
		    $this->ci->em->flush();	
			
			$this->ci->em->persist($inbox);
		    $this->ci->em->flush();	
			
			$this->ci->em->persist($berkasActivity);
		    $this->ci->em->flush();	
			return TRUE;
		} catch (Exception $e){
			return FALSE;
		}
	}
	
	public function nextInboxStep($inbox, $profile, $condition){
		$criteria = array('origin' => $inbox->getProcedureStep(), 'condition' => $condition);
		$procedureTransition = $this->ci->em->getRepository('\models\entities\ProcedureTransition')->findOneBy($criteria);
		echo $inbox->getProcedureStep()->getId(). '-' .$condition;
		
		
		if ($procedureTransition != NULL){
			if ($procedureTransition->getDest() == NULL){
				// FINISH
				$criteria = array('id'=> $inbox->getId());
				$berkas = $this->ci->em->getRepository('\models\entities\Berkas')->findOneBy($criteria);
				$user = CurrentUser::user();
				$berkas->setApprover($user->getUserId());
				$berkas->setEndDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
				$berkas->setResult("FINISH");
				
				$berkasActivity = new \models\entities\BerkasActivity;
				$berkasActivity->setBerkas($berkas);
				$berkasActivity->setProcedureStep($inbox->getProcedureStep());
				$berkasActivity->setProfile($profile);
				$berkasActivity->setAction("FINISH");
				$user = CurrentUser::user();
				$berkasActivity->setUser($user->getUserId());
				
				try {
					$this->ci->em->persist($berkasActivity);
				    $this->ci->em->flush();
										
					$this->ci->em->persist($berkasActivity);
				    $this->ci->em->flush();	
					
					$this->ci->em->remove($inbox);
				    $this->ci->em->flush();	
					return TRUE;
				} catch (Exception $e){
					return FALSE;
				}
			} else {
				$criteria = array('id'=> $procedureTransition->getDest());
				$procedureStep = $this->ci->em->getRepository('\models\entities\ProcedureStep')->findOneBy($criteria);
				$inbox->setProcedureStep($procedureStep);
				$inbox->setCondition($procedureTransition->getCondition());
				$criteria = array('id'=> $inbox->getId());
				$berkas = $this->ci->em->getRepository('\models\entities\Berkas')->findOneBy($criteria);
				
				$isSpecialFlow = FALSE;
				if ($berkas->getTipe()->getId() == "TRAINING" && $condition == "NOK"){
					$criteria = array('id'=> $berkas);
					$berkasTraining = $this->ci->em->getRepository('\models\entities\BerkasTraining')->findOneBy($criteria);
					if ($berkasTraining->getJenisTraining()->getJenisFlow() == 2){
						$isSpecialFlow = TRUE;
						$inbox->setJumlahVerify(0);
						$inbox->setJumlahApprover(0);
						$inbox->setLastVer(NULL);
						$inbox->setLastApp(NULL);
					}
				}
				
				$berkasActivity = new \models\entities\BerkasActivity;
				$berkasActivity->setBerkas($berkas);
				$berkasActivity->setProcedureStep($procedureStep);
				$berkasActivity->setProfile($profile);
				$berkasActivity->setAction($procedureTransition->getCondition());
				$user = CurrentUser::user();
				$berkasActivity->setUser($user->getUserId());
				
				try {
					$this->ci->em->persist($inbox);
				    $this->ci->em->flush();	
					
					$this->ci->em->persist($berkasActivity);
				    $this->ci->em->flush();	
					return TRUE;
				} catch (Exception $e){
					return FALSE;
				}
			}
		}
		
	}
	
}
